package com.sgb.capital.view.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.sgb.capital.R;
import com.sgb.capital.base.BaseActivity;
import com.sgb.capital.callback.Constants;
import com.sgb.capital.callback.ZMessageEvent;
import com.sgb.capital.databinding.BankcardsFragmentBinding;
import com.sgb.capital.model.BankBean;
import com.sgb.capital.utils.Utils;
import com.sgb.capital.view.pop.CardsPop;
import com.sgb.capital.view.pop.TipPop;
import com.sgb.capital.view.ui.activity.BindCompanyBackActivity;
import com.sgb.capital.view.ui.activity.BindUserBackActivity;
import com.sgb.capital.view.ui.activity.CompanyBankCardsActivity;
import com.sgb.capital.view.ui.activity.OpenAccountFirstActivity;
import com.sgb.capital.view.ui.activity.PhoneNumberActivity;
import com.sgb.capital.view.ui.adapter.BankCardsItemAdapter;
import com.sgb.capital.view.widget.MToast;
import com.sgb.capital.viewmodel.BankCardsModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

/**
 * 作者:张磊
 * 日期:2021/4/16 0016
 * 说明:企业银行
 */
public class BankCardsBindFragment extends AppLazyFragment {


    BankCardsModel mModel;
    private BankcardsFragmentBinding mBinding;
    private BankCardsItemAdapter mBankCardsItemAdapter;
    private List<BankBean> mBeans = new ArrayList<>();
    private CardsPop mCardsPop;
    private TipPop mTipPop;
    private String mData;
    private BankBean mDel;
    private BankBean mBankBean;
    private CompanyBankCardsActivity mActivity;
    private boolean isDiss;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.bankcards_fragment, container, false);
        return mBinding.getRoot();
    }

    @Override
    protected void initView() {
        // TODO: 2021/6/17 0017
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        mActivity = (CompanyBankCardsActivity) getActivity();
        mBinding.rvList.setLayoutManager(new LinearLayoutManager(Utils.getContext()));
        mCardsPop = new CardsPop(getActivity());
        mTipPop = new TipPop(getActivity(), Utils.getString(R.string.capital_tip), "解绑后，支付时无法快捷选择银行卡", "解绑", "取消");
        mTipPop.mTvSure.setTextColor(Utils.getColor(R.color.color_FF2928));
        mTipPop.mTvCancel.setTextColor(Utils.getColor(R.color.color_5792FD));
        mModel = new ViewModelProvider(this).get(BankCardsModel.class);
        mBankCardsItemAdapter = new BankCardsItemAdapter(mBeans, false);
    }


    @Override
    public void initObserve() {
        mModel.mCode.observe(this, s -> {
            if (Utils.isEmpty(s)) {
                mModel.getBindBankCardLists((BaseActivity) getActivity());
            } else {
                mTipPop.initStr(s.equals("E000001") ? "银行卡不存在" :
                        s.equals("E000002") ? "非法操作" :
                                s.equals("E000003") ? "当前银行卡状态已发生变化" :
                                        s.equals("E000004") ? "请确保提现银行卡至少保留一张以上" :
                                                "请确保该提现银行卡目前没有处理中的提现业务后，再进行解绑！", null, "知道了");
                mTipPop.show();
            }
        });

        mModel.mBusinessInfoByCompanyEntity.observe(this, businessInfoByCompanyEntity -> {
            if (businessInfoByCompanyEntity == null ) {
                MToast.showToast(Utils.getContext(), "数据异常");
                return;
            }
            if (businessInfoByCompanyEntity.subMerType.contains("3")) {
                BindCompanyBackActivity.start(mActivity);
            } else {
                BindUserBackActivity.start(mActivity, new Gson().toJson(businessInfoByCompanyEntity));
            }
        });
        mModel.mBindBankCardLists.observe(this, bankBeans -> {
            mBankCardsItemAdapter.setNewInstance(bankBeans);
            mBankCardsItemAdapter.notifyDataSetChanged();
        });
    }

    public void initListener() {
        mTipPop.mTvCancel.setOnClickListener(v -> {
            isDiss = true;
            mTipPop.dismiss();
            if (mTipPop.mTvCancel.getText().toString().contains("知道了")) {
                mModel.getBindBankCardLists((BaseActivity) getActivity());
            }
        });

        mTipPop.mTvSure.setOnClickListener(v -> {
            mTipPop.dismiss();
            if (mDel.bindBank) {
                mModel.apply((BaseActivity) getActivity(), mDel.id);
            } else {
                mModel.payBankCard((BaseActivity) getActivity(), mDel.id);
            }
        });
        mBankCardsItemAdapter.setOnItemClickListener((adapter, view, position) -> {
            mBankBean = mBankCardsItemAdapter.getData().get(position);
            if (TextUtils.isEmpty(mBankBean.openingBank)) {
                mCardsPop.mBean = mBankBean;
                mCardsPop.show();
            }
        });
        mBinding.llBtn.setOnClickListener(v -> {
            CompanyBankCardsActivity companyBankCardsActivity = mActivity;
            int position = companyBankCardsActivity.mProcess;
            if (position == 0 || position == 3 || position == 1) {
                TipPop tipPop = new TipPop(mActivity);
                tipPop.initStr(Utils.getString(R.string.capital_tip),
                        position == 1 ? ("企业钱包开通审核中\n通过后可使用此功能") : "开通企业钱包后\n可使用此功能", (position == 1 || position == 2) ? null : "前往开通", "知道了");
                tipPop.show();
                tipPop.mTvSure.setOnClickListener(v1 -> {
                    tipPop.dismiss();
                    OpenAccountFirstActivity.start(mActivity, false);
                });
                return;
            }
            mModel.getBusinessInfoByCompany(mActivity);
        });
    }

    @Override
    protected void onLazyLoad() {
        mBankCardsItemAdapter.mIsPay = false;
        mBinding.rvList.setAdapter(mBankCardsItemAdapter);
        mModel.getBindBankCardLists((BaseActivity) getActivity());

    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe
    public void getCode(ZMessageEvent event) {
        switch (event.code) {
            case Constants.MODIFY_THE_PHONE_NUMBER:
                mCardsPop.dismiss();
                BankBean data = (BankBean) event.data;
                PhoneNumberActivity.start(getActivity(), data.mobileId, data.id);
                break;
            case Constants
                    .EVENT_UNBIND:
                mDel = (BankBean) event.data;
                if (mDel.bindBank) {
                    mTipPop.initStr(mDel.bindBank ? "解绑后，该银行卡将无法用于提现" : "解绑后,该银行卡将无法用于付款", "解绑", "取消");
                    mTipPop.show();
                }
                break;
            case Constants
                    .EVENT_REFRESH:
                System.out.println("提现卡");
                mModel.getBindBankCardLists((BaseActivity) getActivity());
                break;
        }
    }


}